<template>
  <div class="secondpageContentstyle">
    <!-- 标题 -->
    <h2>{{ datas.text }}</h2>
    <span style="margin: 10px 0; display: block; font-weight: bold">组件高度：{{ commentsHeight }}</span>
    <!-- 表单 -->
    <el-form label-width="80px" :model="datas" size="small" :rules="rules">
      <!-- 标题内容 -->
      <el-form-item class="lef" label="选择模板">
        <p style="color: #000; line-height: 4px">{{ styleText }}</p>
      </el-form-item>

      <!-- 卡片样式选择 -->
      <div class="commodityType">
        <el-tooltip v-for="(item, index) in commodityTypes" :key="index" class="item" effect="dark" :content="item.content" placement="bottom">
          <span class="iconfont" style="font-size: 21px" :class="[item.type === datas.commodityType ? 'active' : '', item.icon]" @click="onTest(index)" />
        </el-tooltip>
      </div>

      <div class="bor" />

      <h5 style="color: #000; font-size: 14px">点击添加</h5>
      <p style="color: #969799; font-size: 12px; margin-top: 10px">鼠标拖拽调整卡片顺序</p>

      <!-- 图片广告 -->
      <div>
        <vuedraggable v-model="datas.imageList" v-bind="dragOptions">
          <transition-group>
            <section v-for="(item, index) in datas.imageList" :key="item + index" class="imgBanner">
              <i class="el-icon-circle-close" @click="deleteimg(index)" />
              <!-- 图片 -->
              <div class="imag">
                <img src="../../../../assets/images/add.png" @click="showUpload('second', index)" alt draggable="false" v-if="!item.src" />
                <img :src="item.src" @click="showUpload('second', index)" alt="" draggable="false" />
              </div>
              <!-- 标题和链接 -->
              <div class="imgText">
                <el-input v-model="item.name" placeholder="标题" size="mini" />
                <div style="height: 5px" />
                <el-input v-model="item.desc" placeholder="描述" size="mini" />
                <div style="height: 5px" />
                <!-- 标题和链接 -->
                <div class="imgTextChild">
                  <!-- 选择类型 -->
                  <select-link @selectThisLink="selectThisLink" @clearThisLink="clearThisLink(index)" :linktypeurl="item.url" :theIndex="index" />
                </div>
                <el-form-item class="lef" label="VIP标识">
                  {{ item.isVipShow ? "显示" : "显示" }}
                  <el-checkbox v-model="item.isVipShow" />
                </el-form-item>
              </div>
            </section>
          </transition-group>
        </vuedraggable>
      </div>

      <!-- 上传图片 -->
      <el-button class="uploadImg" type="primary" plain @click="addContent">
        <i class="el-icon-plus" />点击添加
      </el-button>

      <div style="height: 10px" />

      <div class="bor" />

      <!-- 文本粗细 -->
      <el-form-item class="lef" label="标题粗细" prop="textWeight" :hide-required-asterisk="true">
        <el-input v-model.number="datas.textWeight" type="number" placeholder="请输入粗细值" />
      </el-form-item>

      <div style="height: 10px" />

      <!-- 图片倒角 -->
      <el-form-item label="图片倒角" class="lef borrediu">
        <el-slider v-model="datas.borderRadius" :max="30" input-size="mini" show-input />
      </el-form-item>
    </el-form>
    <!-- 上传图片 -->
    <uploadimg ref="upload" @uploadInformation="uploadInformation" />
  </div>
</template>
<script>
import vuedraggable from "vuedraggable"; // 拖拽组件
import uploadimg from "../../../uploadImg"; // 图片上传
import SelectLink from "../../../selectLink";
export default {
  name: "SecondpageContentstyle",
  components: { 
    vuedraggable, 
    uploadimg, 
    SelectLink 
  },
  props: {
    datas: Object,
  },
  data() {
    const kon = (rule, value, callback) => {
      if (value.length === 0) callback(new Error("请输入有效数字"));
    };
    return {
      options1: [],
      commodityTypes: [
        {
          icon: "icon-tupian",
          type: 0,
          content: "一列一个",
        },
        {
          icon: "icon-commodity-yihangliangge",
          type: 1,
          content: "一列两个",
        }
      ],
      rules: {
        textWeight: [{ required: true, validator: kon, trigger: "blur" }],
      },
      dragOptions: {
        animation: 200,
      },
      options: [], // 后端返回的列表提供下拉选择
      emptyText: "",
      changeType: "",
      changeIndex: "",
      newResIdArr: [],
    };
  },
  computed: {
    styleText() {
      if (this.datas.commodityType === 0) return "一列一个";
      if (this.datas.commodityType === 1) return "一列两个";
    },
    commentsHeight() {
      if (this.datas.commodityType === 0) return (this.datas.componHight = 364);
      if (this.datas.commodityType === 1) return (this.datas.componHight = 173);
    },
  },
  created() {},
  methods: {
    onTest(index) {
      this.datas.commodityType = index;
      this.$emit("updateView");
    },
    selectType(index) {
      // 每次切换类型之前 清空之前选中跳转
      this.datas.imageList[index].src = {};
      // 清空 options
      this.options = [];
    },
    addContent() {
      this.datas.imageList.push({
        src: "",
        name: "这里显示标题最多显示2行",
        desc: "这里显示描述",
        type: "1",
        id: "",
        isVipShow:false
      });
    },
    showUpload(type, index) {
      this.changeType = type;
      this.changeIndex = index;
      this.$refs.upload.showUpload();
    },
    // 提交
    uploadInformation(res) {
      if (this.changeType == "first") {
        this.datas.imageList.push({
          src: res,
          name: "这里显示标题最多显示2行",
          desc: "这里显示描述",
          type: "1",
          isVipShow:false
        });
      } else {
        this.datas.imageList[this.changeIndex].src = res;
      }
    },
    // 选择链接
    selectThisLink(resu) {
      this.datas.imageList.forEach((it, ind) => {
        if (ind == resu.index) {
          it.url = resu.url;
          it.linktype = resu.linktype;
          it.name = resu.name;
          it.desc = resu.note;
          it.id = resu.id;
        }
      });
    },
    clearThisLink (index) {
      this.datas.imageList[index].url = '';
    },
    /* 删除图片 */
    deleteimg(index) {
      this.datas.imageList.splice(index, 1);
    },
  },
};
</script>
<style lang="scss" scoped>
.secondpageContentstyle {
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  padding: 0 10px 20px;
  box-sizing: border-box;
  .bor {
    height: 1px;
    margin: 20px 0;
    background-color: #ebedf0;
  }
  /* 标题 */
  h2 {
    padding: 24px 16px 24px 0;
    margin-bottom: 15px;
    border-bottom: 1px solid #f2f4f6;
    font-size: 18px;
    font-weight: 600;
    color: #323233;
  }

  .lef {
    .el-form-item__label {
      text-align: left;
    }
  }

  /* 图片字 */
  .imgText1 {
    width: 100%;
    display: flex;
    box-sizing: border-box;
    justify-content: space-between;
    .fir-sele.el-select {
      width: 40%;
    }
  }

  /* 列表样式 */
  .commodityType {
    display: flex;
    justify-content: space-around;
    align-items: center;
    span {
      display: inline-block;
      width: 58px;
      height: 32px;
      text-align: center;
      line-height: 32px;
      background: #fff;
      border: 1px solid #ebedf0;
      color: #979797;
      margin: 0 1px;
      cursor: pointer;
      transition: all 0.5s;

      &:hover {
        border: 1px solid #155bd4;
        color: #155bd4;
      }

      &.active {
        border: 1px solid #155bd4;
        background-color: #e0edff;
        color: #155bd4;
      }
    }
  }

  /* 卡片样式 */
  .moditystyle {
    font-size: 12px;
    width: 100%;
    display: flex;
    span {
      width: 86px;
      height: 32px;
      display: inline-block;
      text-align: center;
      line-height: 32px;
      cursor: pointer;
      border: 1px solid #ebedf0;
      &.active {
        border: 1px solid #155bd4;
        background-color: #e0edff;
        color: #155bd4;
      }
    }
  }

  /* 位置 */
  .weiz {
    text-align: right;
    i {
      padding: 5px 14px;
      margin-left: 10px;
      border-radius: 0;
      border: 1px solid #ebedf0;
      font-size: 20px;
      font-weight: 500;
      cursor: pointer;

      &:last-child {
        font-size: 22px;
      }

      &.active {
        color: #155bd4;
        border: 1px solid #155bd4;
        background: #e0edff;
      }
    }
  }

  /* 单选框 */
  .radi1 {
    border-top: 1px solid #f7f8fa;
    border-bottom: 1px solid #f7f8fa;
    padding: 12px 0;
    .el-radio {
      margin: 10px 25px 7px 0;
    }
  }

  /* 卡片列表 */
  .imgBanner {
    padding: 6px 12px;
    margin: 16px 7px;
    border-radius: 2px;
    background-color: #fff;
    box-shadow: 0 0 4px 0 rgba(10, 42, 97, 0.2);
    display: flex;
    position: relative;

    /* 删除图标 */
    .el-icon-circle-close {
      position: absolute;
      right: -10px;
      top: -10px;
      cursor: pointer;
      font-size: 19px;
    }

    /* 图片 */
    .imag {
      width: 90px;
      height: 90px;
      border-radius: 5px;
      overflow: hidden;
      position: relative;
      cursor: pointer;
      img {
        width: 100%;
        height: 100%;
        display: inline-block;
      }
      span {
        background: rgba(0, 0, 0, 0.5);
        font-size: 12px;
        position: absolute;
        left: 0px;
        bottom: 0px;
        display: inline-block;
        width: 100%;
        text-align: center;
        color: #fff;
        height: 20px;
        line-height: 20px;
      }
    }

    /* 图片字 */
    .imgText {
      width: 80%;
      display: flex;
      flex-direction: column;
      box-sizing: border-box;
      padding-left: 10px;
      justify-content: space-between;
      /* 图片字 */
      .imgTextChild {
        width: 100%;
        display: flex;
        box-sizing: border-box;
        justify-content: space-between;
        .fir-sele.el-select {
          width: 40%;
        }
      }
    }
  }

  /* 上传图片按钮 */
  .uploadImg {
    width: 345px;
    height: 40px;
    margin-top: 20px;
  }

  // 上传弹框内容部分
  .uploadIMG .el-dialog__body {
    height: 280px;
    display: flex;
    flex-direction: column;
    align-items: center;
    position: relative;
    justify-content: center;
  }

  .disable {
    .el-upload {
      display: none !important;
    }
  }

  .tit {
    .el-input__inner {
      text-align: center;
    }
  }
}
</style>
